package com.micro.ai.commons.client;

import com.micro.ai.commons.domain.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * 基础客户端接口
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@FeignClient(
    name = "base-client",
    fallbackFactory = BaseClient.BaseClientFallbackFactory.class
)
public interface BaseClient {

    /**
     * GET 请求
     */
    @GetMapping("/api/test")
    Result<String> testGet(@RequestParam("param") String param);

    /**
     * POST 请求
     */
    @PostMapping("/api/test")
    Result<String> testPost(@RequestBody Map<String, Object> data);

    /**
     * 降级处理工厂
     */
    @Slf4j
    class BaseClientFallbackFactory implements FallbackFactory<BaseClient> {

        @Override
        public BaseClient create(Throwable cause) {
            log.error("BaseClient 调用失败", cause);
            return new BaseClient() {
                @Override
                public Result<String> testGet(String param) {
                    return Result.error("服务不可用");
                }

                @Override
                public Result<String> testPost(Map<String, Object> data) {
                    return Result.error("服务不可用");
                }
            };
        }
    }
}
